import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../theme/app_colors.dart';

class ThemeManager with ChangeNotifier {
  static const String _themeKey = 'app_theme';
  static const String darkTheme = 'dark';
  static const String lightTheme = 'light';
  
  String _currentTheme = darkTheme;

  String get currentTheme => _currentTheme;

  bool get isDarkMode => _currentTheme == darkTheme;
  bool get isLightMode => _currentTheme == lightTheme;

  ThemeManager() {
    _loadTheme();
  }

  Future<void> _loadTheme() async {
    final prefs = await SharedPreferences.getInstance();
    _currentTheme = prefs.getString(_themeKey) ?? darkTheme;
    notifyListeners();
  }

  Future<void> setTheme(String theme) async {
    _currentTheme = theme;
    final prefs = await SharedPreferences.getInstance();
    await prefs.setString(_themeKey, theme);
    notifyListeners();
  }

  Future<void> toggleTheme() async {
    if (_currentTheme == darkTheme) {
      await setTheme(lightTheme);
    } else {
      await setTheme(darkTheme);
    }
  }

  ThemeData get themeData {
    if (_currentTheme == lightTheme) {
      const colors = AppColors.light;
      return ThemeData(
        brightness: Brightness.light,
        primarySwatch: Colors.blue,
        primaryColor: colors.primary,
        scaffoldBackgroundColor: colors.scaffoldBackground,
        cardColor: colors.cardBackground,
        dividerColor: colors.divider,
        colorScheme: ColorScheme.light(
          primary: colors.primary,
          secondary: colors.secondary,
          surface: colors.surface,
          background: colors.background,
          error: colors.error,
          onPrimary: colors.onPrimary,
          onSecondary: colors.onSecondary,
          onSurface: colors.onSurface,
          onBackground: colors.onBackground,
          onError: colors.onPrimary,
          brightness: Brightness.light,
        ),
        appBarTheme: AppBarTheme(
          backgroundColor: colors.primary,
          foregroundColor: colors.onPrimary,
          iconTheme: IconThemeData(color: colors.onPrimary),
        ),
        cardTheme: CardThemeData(
          color: colors.cardBackground,
          elevation: 2,
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(8),
            side: BorderSide(color: colors.border, width: 1),
          ),
        ),
        switchTheme: SwitchThemeData(
          thumbColor: MaterialStateProperty.resolveWith((states) {
            if (states.contains(MaterialState.selected)) {
              return colors.switchActiveThumb;
            }
            return colors.switchInactiveThumb;
          }),
          trackColor: MaterialStateProperty.resolveWith((states) {
            if (states.contains(MaterialState.selected)) {
              return colors.switchActiveTrack;
            }
            return colors.switchInactiveTrack;
          }),
        ),
        elevatedButtonTheme: ElevatedButtonThemeData(
          style: ElevatedButton.styleFrom(
            backgroundColor: colors.buttonPrimary,
            foregroundColor: colors.onPrimary,
          ),
        ),
        textTheme: TextTheme(
          bodyLarge: TextStyle(color: colors.textPrimary),
          bodyMedium: TextStyle(color: colors.textSecondary),
          bodySmall: TextStyle(color: colors.textHint),
        ),
      );
    } else {
      const colors = AppColors.dark;
      return ThemeData(
        brightness: Brightness.dark,
        primarySwatch: Colors.blue,
        primaryColor: colors.primary,
        scaffoldBackgroundColor: colors.scaffoldBackground,
        cardColor: colors.cardBackground,
        dividerColor: colors.divider,
        colorScheme: ColorScheme.dark(
          primary: colors.primary,
          secondary: colors.secondary,
          surface: colors.surface,
          background: colors.background,
          error: colors.error,
          onPrimary: colors.onPrimary,
          onSecondary: colors.onSecondary,
          onSurface: colors.onSurface,
          onBackground: colors.onBackground,
          onError: colors.onPrimary,
          brightness: Brightness.dark,
        ),
        appBarTheme: AppBarTheme(
          backgroundColor: colors.scaffoldBackground,
          foregroundColor: colors.textPrimary,
          iconTheme: IconThemeData(color: colors.textPrimary),
        ),
        cardTheme: CardThemeData(
          color: colors.cardBackground,
          elevation: 2,
          shape: RoundedRectangleBorder(
            borderRadius: const BorderRadius.all(Radius.circular(12)),
            side: BorderSide(color: colors.border, width: 1),
          ),
        ),
        switchTheme: SwitchThemeData(
          thumbColor: MaterialStateProperty.resolveWith((states) {
            if (states.contains(MaterialState.selected)) {
              return colors.switchActiveThumb;
            }
            return colors.switchInactiveThumb;
          }),
          trackColor: MaterialStateProperty.resolveWith((states) {
            if (states.contains(MaterialState.selected)) {
              return colors.switchActiveTrack;
            }
            return colors.switchInactiveTrack;
          }),
        ),
        elevatedButtonTheme: ElevatedButtonThemeData(
          style: ElevatedButton.styleFrom(
            backgroundColor: colors.buttonPrimary,
            foregroundColor: colors.onPrimary,
          ),
        ),
        textTheme: TextTheme(
          bodyLarge: TextStyle(color: colors.textPrimary),
          bodyMedium: TextStyle(color: colors.textSecondary),
          bodySmall: TextStyle(color: colors.textHint),
        ),
      );
    }
  }
}